package com.hhkj.admin.order.common.component.validator;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.order.common.exception.InvalidOrderItemException;
import com.hhkj.admin.order.domain.OrderItem;
import com.hhkj.admin.student.domain.StudentCourse;
import com.ruoyi.common.base.model.extend.ValidError;
import lombok.AllArgsConstructor;

import java.util.*;


public class StudentValidateStrategy extends BaseOrderItemValidateStrategy{

    /** 获取（身份证-已报名科目）map一映射表 **/
    private Map<String, List<StudentCourse>> idCardStudentCourseMap;

    /**（身份证-名称）map映射表 **/
    private Map<String, String> studentNameMap;

    /** 判断学员是否重复map表 **/
    private Map<String, Set<String>> map;

    public StudentValidateStrategy(Map<String, List<StudentCourse>> idCardStudentCourseMap, Map<String, String> studentNameMap) {
        this.idCardStudentCourseMap = idCardStudentCourseMap;
        this.studentNameMap = studentNameMap;
        map = new HashMap<>();
    }

    /**
     * 校验该学员是否可以录入
     * @param orderItem
     * @return
     * @throws InvalidOrderItemException
     */
    @Override
    public boolean validate(OrderItem orderItem) throws InvalidOrderItemException {
        //检测学员是否已经报名
        List<StudentCourse> studentCourseList = idCardStudentCourseMap.get(orderItem.getIdCard());
        OrderItemValidator.isStudentNotEnroll(studentCourseList, orderItem.getCourseId());
        //检测学员是否同身份证，不同名字
//        OrderItemValidator.isDifferentName(orderItem.getName(), studentNameMap.get(orderItem.getIdCard()));
        //校验订单中学员是否重复录入
        Set<String> courseNameSet = map.get(orderItem.getIdCard());
        if (ObjectUtil.isEmpty(courseNameSet)) {
            courseNameSet = new HashSet<>();
            map.put(orderItem.getIdCard(), courseNameSet);
        } else if (courseNameSet.contains(orderItem.getCourseName())) {
            throw new InvalidOrderItemException("有学员重复录入");
        }
        courseNameSet.add(orderItem.getCourseName());
        return true;
    }
}
